package nimy.info.android.dinnertime.adapter;

import java.util.List;

import nimy.info.android.dinnertime.R;
import nimy.info.android.dinnertime.model.RecipeKeyword;
import nimy.info.android.dinnertime.utils.Utils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * Help to render the recipe keywords list
 * 
 * @author 234aini@gmail.com
 * 
 */
public class RecipeKeywordItemAdapter extends ArrayAdapter<RecipeKeyword> {

	private LayoutInflater mInflater;

	public RecipeKeywordItemAdapter(final Context context) {
		super(context, R.layout.list_item_recipe_keywords);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			view = mInflater.inflate(R.layout.list_item_recipe_keywords,
					parent, false);
		} else {
			view = convertView;
		}
		RecipeKeyword item = (RecipeKeyword) getItem(position);

		TextView nameView = (TextView) view
				.findViewById(R.id.recipe_keyword_name);
		nameView.setText(item.getName());

		// colorful the keywords
		nameView.setTextColor(Utils.getRandomColor(item.getCount()));

		TextView indexView = (TextView) view
				.findViewById(R.id.recipe_keyword_index);
		indexView.setText(Integer.toString(position + 1));

		TextView countView = (TextView) view
				.findViewById(R.id.recipe_keyword_count);
		countView.setText(Integer.toString(item.getCount()));
		return view;
	}

	public void setModel(List<RecipeKeyword> model, final boolean clear) {
		if (clear) {
			clear();
		}
		if (model != null && !model.isEmpty()) {
			for (RecipeKeyword keyword : model) {
				add(keyword);
			}
		}
	}

}
